package com.md.context;


import lombok.experimental.UtilityClass;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 刘骄阳
 * 2022-04-06 17:09
 */
@UtilityClass
public class LogServerContext {

  private static final ThreadLocal<ConcurrentHashMap<String, Object>> CONTEXT_HOLDER =
    new InheritableThreadLocal<ConcurrentHashMap<String, Object>>() {
      @Override
      protected ConcurrentHashMap<String, Object> initialValue() {
        return new ConcurrentHashMap<>();
      }
    };

  public static Object get(final String key) {
    return CONTEXT_HOLDER.get().get(key);
  }

  public static String getString(final String key) {
    return (String) CONTEXT_HOLDER.get().get(key);
  }

  public static Long getLong(final String key) {
    return (Long) CONTEXT_HOLDER.get().get(key);
  }

  public static void put(final String key, final Object value) {
    CONTEXT_HOLDER.get().put(key, value);
  }

  public static void clear() {
    CONTEXT_HOLDER.remove();
  }
}
